<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/17
 * Time: 下午5:21
 */

namespace app\common\model;

use JPush\Client;
use think\Db;

class Message extends Common
{
    /**
     * 极光推送
     *
     * @param string $message 消息内容
     * @param array $extra 额外数据
     * @param array $user 用户
     * @throws ServerException
     */
    public static  function jPush($message, $user = array(),$extra=array())
    {
        //初始化JPush客户端
        $client = new Client(config('jpush')["app_key"], config('jpush')["app_secret"]);

        //生成推送
        $push = $client->push();

        //设置推送平台
        $push->setPlatform("all");

        //设置推送目标
        if (empty($user))
        {
            $push->addAllAudience();
        }
        //向指定用户推送
        else
        {
//            if (is_array($user))
//            {
//                $user = implode(",", $user);
//            }
            $push->addAlias($user);
        }

       $push->iosNotification($message, array("extras" => $extra))
       ->androidNotification($message, array("extras" => $extra));
      // $push->message($message, array("extras" => $extra));


        //发送推送报文
        try
        {
            $push->send();
        }
        catch (\Exception $exception)
        {
//            throw new ServerException($exception->getMessage());
        }
    }

    /**
     * 后台操作log
     * @param array $admin
     * @param int $money
     * @param string $extra
     */
    static public function admin_log($admin,$extra,$money=0)
    {
        db('admin_log')
            ->insert(array(
                'admin_id'=>$admin['id'],
                'user_id'=>$admin['uid'],
                'user_name'=>$admin['username'],
                'money'=>$money,
                'content'=>$extra,
                'create_time'=>now_datetime()
            ));
    }

    /**
     * 公告列表
     * @param $bussiness_id
     * @param $page
     * @param $pagesize
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    static public function noticeList($bussiness_id,$page,$pagesize)
    {
        if($bussiness_id=='-1')
        {
            $list=Db::name('notice')
                ->where('is_visible',1)
                ->order('display_order desc,update_time desc')
                ->page($page,$pagesize)
                ->select();
        }else{
            $list=Db::name('notice')
                ->where('bussiness_id',$bussiness_id)
                ->where('is_visible',1)
                ->order('display_order desc,update_time desc')
                ->page($page,$pagesize)
                ->select();
        }

        return empty($list)?array():$list;
    }

    /**
     * 公告详情
     * @param $notice_id
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    static public function noticeInfo($notice_id)
    {
            $info=Db::name('notice')
                ->where('id',$notice_id)
                ->where('is_visible',1)
                ->field('bussiness_id,name,detail,update_time')
                ->find();

        return empty($info)?array():$info;
    }

    /**
     * 新闻列表
     * @param $bussiness_id
     * @param int $city
     * @param int $page
     * @param int $pagesize
     * @param $category_id
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function newsList($bussiness_id,$city,$page,$pagesize,$category_id=0)
    {
        if(empty($city))
        {
            $city="182";
        }
        if($category_id)
        {
            $list=Db::name('content_article')
                ->where('bussiness_id',$bussiness_id)
                ->where('is_visible',1)
                ->where('category_id',$category_id)
                ->where('city',$city)
                ->order('update_time desc')
                ->page($page,$pagesize)
                ->select();
        }else{
            $list=Db::name('content_article')
                ->where('bussiness_id',$bussiness_id)
                ->where('is_visible',1)
                ->where('city',$city)
                ->where('is_recommend',1)
                ->order('update_time desc')
                ->page($page,$pagesize)
                ->select();
        }

        return empty($list)?array():$list;
    }

    /**
     * 新闻详情
     * @param $news_id
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    static public function newsInfo($news_id)
    {
        $info=Db::name('content_article')
            ->where('id',$news_id)
            ->where('is_visible',1)
            ->field('bussiness_id,city,category_id,title,thumb,description,detail,update_time,view_num')
            ->find();
        /*增加浏览量*/
        if(!empty($info))
        {
            Db::name('content_article')
                ->where('id',$news_id)
                ->setInc('view_num');
        }

        return empty($info)?array():$info;
    }

    /**
     * 轮播列表
     * @param $type
     * @param int $city
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    static public function bannerList($type,$city)
    {
        if(empty($city))
        {
            $city="182";
        }
        $list=Db::name('swiper')
            ->where('category_id',$type)
            ->where('is_visible',1)
            ->where('city',$city)
            ->order('display_order desc')
            ->select();
        return empty($list)?array():$list;
    }
    /**
     * 活动列表
     * @param int $pagesize
     * @param int $page
     * @param int $is_recommend (收否推荐-1：全部)
     * @param int $city
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    static public function activityList($is_recommend,$city,$page,$pagesize)
    {
        if(empty($city))
        {
            $city="182";
        }
        if($is_recommend=="-1")
        {/*全部列表*/
            $list=Db::name('active')
                ->where('is_visible',1)
                ->where('city',$city)
                ->order('display_order desc,update_time desc')
                ->page($page,$pagesize)
                ->select();
        }else{
            /*推荐列表*/
            $list=Db::name('active')
                ->where('is_visible',1)
                ->where('city',$city)
                ->where('is_recommend',$is_recommend)
                ->order('display_order desc,update_time desc')
                ->page($page,$pagesize)
                ->select();
        }
        if(!empty($list))
        {
            foreach ($list as &$item)
            {
                $item['begintime']=show_datetime($item['begintime']);
                $item['endtime']=show_datetime($item['endtime']);
            }
        }


        return empty($list)?array():$list;
    }
    /**
     * 我的活动列表
     * @param int $pagesize
     * @param int $page
     * @param int $type
     * @param array $user
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    static public function myActive($user,$type,$page,$pagesize)
    {
        if($type==1){
            /*活动列表*/
            $list=Db::name('active')
                ->alias('ac')
                ->join('__ORDER__ or','ac.id=or.active_id')
                ->where('or.pay_status',1)
                ->where('ac.is_visible',1)
                ->where('or.uid',$user['id'])
                ->order('ac.display_order desc,ac.update_time desc')
                ->page($page,$pagesize)
                ->field('ac.*')
                ->select();
        }else{
            /*活动列表*/
            $list=Db::name('active')
                ->where('is_visible',1)
                ->where('uid',$user['id'])
                ->order('display_order desc,update_time desc')
                ->page($page,$pagesize)
                ->field('id,uid,union_id,city,title,type,thumb,begintime,endtime,signstop_time,address,coin,money,detail,status')
                ->select();
        }

        return empty($list)?array():$list;
    }

    /**
     * 活动详情
     * @param $active_id
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function activityInfo($active_id)
    {
        $info=Db::name('active')
            ->where('id',$active_id)
            ->where('is_visible',1)
            ->field('id,uid,city,address,title,thumb,detail,begintime,endtime,union_id,signstop_time,money,coin,background')
            ->find();

        return empty($info)?array():$info;
    }


}
